package com.cat.common;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ？
 *
 * @author xiemh
 * @date 2018/12/13 14:01
 * @since v1.0.0
 */
public class SpiderUtils {
    private static final ThreadLocal<SimpleDateFormat> format = new ThreadLocal<>();
    private static final String PRE = "/app/spider/img/";

    public static String genPath(String channel, String word, String md5, String type) {
        SimpleDateFormat dateFormat = format.get();
        if (dateFormat == null) {
            dateFormat = new SimpleDateFormat("yyyyMMdd");
            format.set(dateFormat);
        }
        String date = dateFormat.format(new Date());

        String name = md5 + "." + type;
        return PRE + channel + "/" + word + "/" + date + "/" + name;
    }

    public static String genPathSmall(String path, String url) {
        String[] split = url.split("\\.");
        String type = "jpg";
        if (split.length == 0) {
            type = split[split.length - 1];
        }
        return path + "_" + "." + type;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        // String dir = genPath("baidu", "def9ce076725919de8d6b51f9b90efa3", "jpg");
        // System.out.println(dir);
        // System.out.println(genPathSmall(dir,"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2403594837,3847831698&fm=26&gp=0.jpg"));

        String s = "ippr://t42f6v.kwt17.v54/t4w2j/va%nDfit37j8%dCa%dCa%dCdl9%dC9a/ft2g=c01luw111umdbcncbmj11wmdubbm8vkn/j911j088laju0mvmmkmm0ka9l08mu1uwwuc8m0m1.3r2";
        String baiduSrcUrl = getBaiduSrcUrl(s);
        System.out.println(baiduSrcUrl);
        // String s1 = URLDecoder.decode(s, "GBK");
        // System.out.println(s1);
        // String baiduSrcUrl2 = getBaiduSrcUrl(s1);
        String baiduSrcUrl2 = getBaiduSrcUrl("ippr://t42f6v.kwt17.v54/t4w2j/va%nDfit37j8aadl99a/ft2g=c01luw111umdbcncbmj11wmdubbm8vkn/j911j088laju0mvmmkmm0ka9l08mu1uwwuc8m0m1.3r2");
        System.out.println(baiduSrcUrl2);

        // System.out.println(URLDecoder.decode("http://imgsrc.baidu.com/image/c0%32shijue1%250%250%25294%2540/sign=57d9fadddf62853586edda62f8861cb3/e4dde71190ef76c66b667b049716fdfaaf51676d.jpg","utf-8"));
    }

    public static String getBaiduSrcUrl(String s) {
        char[] a1 = "0123456789abcdefghijklmnopqrstuvw".toCharArray();
        char[] a2 = "7dgjmoru140852vsnkheb963wtqplifca".toCharArray();
        String result = "";
        // s = URLDecoder.decode(s,"UTF-8");
        s = s.replace("_z2C$q", ":").replace("_z&e3B", ".").replace("AzdH3F", "/");
        for (char i : s.toLowerCase().toCharArray()) {
            for (int j = 0; j < a1.length; j++) {
                if (i == a1[j]) {
                    result += a2[j];
                    break;
                }
                if (j == a1.length - 1) {
                    result += i;
                }
            }
        }
        return result;
    }

    public static void downloadPicture(String remoteUrl, String localPath) {
        URL url = null;
        int imageNumber = 0;

        try {
            url = new URL(remoteUrl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(localPath));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context = output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
